const { option } = require("yargs");
const { vital, log, warn } = require("./log_tool");
const { exec_shell } = require("./shell_tool");
const path = require("node:path");
const fs = require("node:fs");
const { copy, get_fst_file, get_full_path } = require("./file_tool");

async function svn_is_dirty(cwd) {
    return await exec_shell(
        {
            cmd: "svn status . --quiet",
            cwd: cwd,
            capture_stdout: true,
        },
        true,
    );
}

async function git_is_dirty(cwd) {
    const result = await exec_shell(
        {
            cmd: "git status .",
            cwd: cwd,
            capture_stdout: true,
        },
        true,
    );
    return (
        result && result.indexOf("nothing to commit, working tree clean") === -1
    );
}

async function git_commit(cwd, msg) {
    await exec_shell(
        {
            cmd: "git add .",
            cwd: cwd,
        },
        true,
    );
    await exec_shell({
        cmd: "git commit -m".split(" ").concat(msg),
        cwd: cwd,
    });
}

async function git_push(cwd, options) {
    const remote =
        options.remote ||
        (await exec_shell({ cmd: "git remote", capture_stdout: true })).trim();
    const branch =
        options.branch ||
        (
            await exec_shell({
                cmd: "git symbolic-ref --short -q HEAD",
                capture_stdout: true,
            })
        ).trim();
    if (!remote || !branch) {
        warn(`在 ${cwd} 中未找到 remote 和 branch, 无法 执行 git push`);
        return;
    }
    let cmd = "git push";
    if (options.remote && options.branch) {
        cmd = `git push --set-upstream ${options.remote} ${options.branch}`;
    }
    await exec_shell({
        cmd: cmd,
        cwd: cwd,
        shell: true,
    });
}

async function git_export_diff_head(cwd, dst, base, options) {
    const diff = await exec_shell(
        {
            // cmd: "git diff HEAD^ HEAD --stat",
            // cmd: "git diff --name-only HEAD~1",
            cmd: "git diff --name-only HEAD^",
            cwd: cwd,
            capture_stdout: true,
        },
        true,
    );
    let git_root = await exec_shell(
        {
            cmd: "git rev-parse --show-toplevel",
            cwd: cwd,
            capture_stdout: true,
        },
        true,
    );
    git_root = git_root.trim();
    if (!git_root) {
        throw "git根目录未找到";
    }
    /** @type {string[]} */
    const lines = diff.split("\n");
    /** @type {string[]} */
    const files = [];
    for (const line of lines) {
        const raw_file = line.trim();
        if (!raw_file) break;
        const file = path.join(git_root, raw_file);
        if (fs.existsSync(file)) {
            files.push(file);
        }
    }
    await copy(files, dst, base, options);
}

async function svn_export_diff_head(cwd, dst, base, options) {
    cwd = get_full_path(cwd);
    dst = get_full_path(dst);
    const diff = await exec_shell(
        {
            cmd: "svn diff -r PREV:HEAD --summarize",
            cwd: cwd,
            capture_stdout: true,
        },
        true,
    );
    const lines = diff.split("\n");
    const files = lines
        .filter((v) => v)
        .map((v) => path.join(cwd, /\w\s+(\S+)/.exec(v)[1]));
    await copy(files, dst, base, options);
}

module.exports = {
    svn_is_dirty,
    git_is_dirty,
    git_commit,
    git_push,
    git_export_diff_head,
    svn_export_diff_head,
};
